package org.itboys.core.service.mysql.web;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.dao.tray.SundayTrayMapper;
import org.itboys.core.dto.tray.SundayTrayDto;
import org.itboys.core.entity.mongo.SundayMember;
import org.itboys.core.entity.mongo.SundayStore;
import org.itboys.core.entity.mongo.SundayTrayEwm;
import org.itboys.core.entity.mysql.tray.SundayTray;
import org.itboys.core.entity.mysql.tray.SundayTrayModel;
import org.itboys.core.service.mongo.web.SundayMemberService;
import org.itboys.core.service.mongo.web.SundayStoreService;
import org.itboys.core.service.mongo.web.SundayTrayEwmService;
import org.itboys.core.util.ZXingCode;
import org.itboys.core.util.ZipUtils;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mysql.service.BaseMySqlService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 作者：zzq
 * 时间：2018/12/5
 * 描述：托盘
 */
@Service
public class SundayTrayService extends BaseMySqlService<SundayTray> {
    @Autowired
    private ResourceHolder resourceHolder;
    @Autowired
    private SundayMemberService memberService;
    @Autowired
    private SundayTrayModelService modelService;
    @Autowired
    private SundayStoreService storeService;
    @Autowired
    private SundayTrayMapper trayMapper;
    @Autowired
    private SundayTrayEwmService ewmService;

    /**
     * 批量新增  保存记录
     *
     * @param modelId
     * @param modelName
     * @param num
     */
    @Transactional
    public void batchSave(Long modelId, String modelName, Integer num) {
        List<SundayTray> trays = new ArrayList<>();
        Long random = Long.valueOf(System.currentTimeMillis());
        for (int i = 0; i < num; i++) {
            random = random + i;
            SundayTray tray = new SundayTray(modelId, 0l, 0l, 0l, 0l, "/ewm/" + random + ".jpg", SundayCommonConstants.tray_status_dfp);
            trays.add(tray);
        }
        trayMapper.batchInsert(trays);
        List<File> srcFiles = new ArrayList<>();
        for (SundayTray tray : trays) {
            String savePath = resourceHolder.getStringValue("fileUploadPath") + tray.getEwm();
            ZXingCode.drawLogoQRCode(null, new File(savePath), String.valueOf(tray.getId()), modelName+ "：" + String.valueOf(tray.getId()));
            srcFiles.add(new File(savePath));
        }
        String zipPath = "/ewmZip/" + random + ".zip";
        ZipUtils.zipFiles(srcFiles, new File(resourceHolder.getStringValue("fileUploadPath") + zipPath));
        ewmService.save(new SundayTrayEwm(modelName, num, zipPath));
    }

    /**
     * 分页查询
     *
     * @param request
     * @return
     */
    public PageResult<SundayTrayDto> select(HttpServletRequest request, List<Long> ids) {
        Map<String, Object> param = ParamUtil.packageMysqlPageParam(request, "status", "modelId", "storeId", "serviceId", "ownerId","id");
        if (ListUtil.isNotNull(ids)) {
            param.put("ids", ids);
        }
        return new PageResult<>(packageMultiDto(super.select(param)), super.count(param));
    }


    /**
     * 组装多个
     *
     * @param trays
     * @return
     */
    public List<SundayTrayDto> packageMultiDto(List<SundayTray> trays) {
        List<SundayTrayDto> dtos = new ArrayList<>();
        List<Long> ownerIds = trays.stream().map(e -> e.getOwnerId()).collect(Collectors.toList());
        List<Long> serviceIds = trays.stream().map(e -> e.getServiceId()).collect(Collectors.toList());
        List<Long> modelIds = trays.stream().map(e -> e.getModelId()).collect(Collectors.toList());
        List<Long> storeIds = trays.stream().map(e -> e.getStoreId()).collect(Collectors.toList());
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("id in", ownerIds);
        List<SundayMember> owners = memberService.list(param);
        param.clear();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("id in", serviceIds);
        List<SundayMember> services = memberService.list(param);
        param.clear();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("ids", modelIds);
        List<SundayTrayModel> models = modelService.select(param);
        param.clear();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("id in", storeIds);
        List<SundayStore> stores = storeService.list(param);


        for (SundayTray tray : trays) {
            SundayTrayDto dto = new SundayTrayDto();
            BeanUtils.copyProperties(tray, dto);

            for (SundayMember member : owners) {
                if (tray.getOwnerId().longValue() == member.getId()) {
                    dto.setOwnerName(member.getNickName());
                    break;
                }
            }
            for (SundayMember member : services) {
                if (tray.getServiceId().longValue() == member.getId()) {
                    dto.setServiceName(member.getNickName());
                    break;
                }
            }
            for (SundayTrayModel model : models) {
                if (tray.getModelId().longValue() == model.getId().longValue()) {
                    dto.setModelName(model.getName());
                    break;
                }
            }
            for (SundayStore store : stores) {
                if (tray.getStoreId().longValue() == store.getId()) {
                    dto.setStoreName(store.getName());
                    break;
                }
            }

            dto.setEwm(resourceHolder.getStringValue("imgRoot") + tray.getEwm());
            dtos.add(dto);
        }
        return dtos;
    }

}
